package edu.zju.gis.dbfg.server.service.impl;

import com.github.pagehelper.PageHelper;
import edu.zju.gis.dbfg.common.Page;
import edu.zju.gis.dbfg.common.base.BaseServiceImpl;
import edu.zju.gis.dbfg.server.mapper.TaskRecordMapper;
import edu.zju.gis.dbfg.server.model.Monitor;
import edu.zju.gis.dbfg.server.model.TaskRecord;
import edu.zju.gis.dbfg.server.service.TaskRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TaskRecordServiceImpl extends BaseServiceImpl<TaskRecordMapper, TaskRecord, String> implements TaskRecordService {
    @Autowired
    private TaskRecordMapper taskRecordMapper;

    @Override
    public TaskRecord select(String applicationId) {
        return taskRecordMapper.selectByPrimaryKey(applicationId);
    }

    public int insert(TaskRecord taskRecord) {
        return taskRecordMapper.insertSelective(taskRecord);
    }

    @Override
    public int update(TaskRecord taskRecord) {
        return taskRecordMapper.updateByPrimaryKeyWithBLOBs(taskRecord);
    }

    @Override
    public int delete(String applicationId) {
        return taskRecordMapper.deleteByPrimaryKey(applicationId);
    }

    @Override
    public boolean isExist(String applicationId) {
        return taskRecordMapper.selectByPrimaryKey(applicationId) != null;
    }

    public List<TaskRecord> getByPage(int offset, int size) {
        return taskRecordMapper.selectByPage(offset, size);
    }

    @Override
    public List<TaskRecord> getByUser(String user) {
        return taskRecordMapper.selectByCreateUser(user);
    }

    @Override
    public List<TaskRecord> getByUserWithState(String user, Monitor.FinalStatus status) {
        return taskRecordMapper.selectByCreateUserWithState(user, status.name());
    }

    @Override
    public List<TaskRecord> getByUserWithoutState(String user, Monitor.FinalStatus status) {
        return taskRecordMapper.selectByCreateUserWithOutState(user, status.name());
    }

    @Override
    public Page<TaskRecord> getPagesByUser(String user, Page page) {
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        return new Page<TaskRecord>(taskRecordMapper.selectByCreateUser(user));
    }
}

